#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int str2time (char s[])
{
    int time, i, t;
    char ss[4];

    time = 0;

    for (i = 0; i < 3; i++) {
        ss[0] = s[0+i*3]; ss[1] = s[1+i*3];ss[2] = '\n';
        t = atoi(ss);

        if (i == 0) {
            t = t * 60 * 60;
        } else if (i == 1) {
            t = t * 60;
        }

        time += t;
    }

    return time;
}

int main (int argc, char const* argv[])
{
    int n, c, first_come_time, last_leave_time, come_time, leave_time;
    char first_come_id[16], last_leave_id[16], id[16], buf[16];

    scanf("%d", &n);
    while (n--) {
        scanf("%d", &c);

        first_come_time = 24 * 60 * 60;
        last_leave_time = 0;

        while (c--) {
            scanf("%s", id);

            scanf("%s", buf);
            come_time = str2time(buf);
            scanf("%s", buf);
            leave_time = str2time(buf);

            if (come_time <= first_come_time) {
                first_come_time = come_time;
                strcpy(first_come_id, id);
            }

            if (leave_time >= last_leave_time) {
                last_leave_time = leave_time;
                strcpy(last_leave_id, id);
            }
        }

        printf("%s %s\n", first_come_id, last_leave_id);
    }

    return 0;
}
